package com.wf.smartcomm.server.admin.action;


import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import org.json.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ModelDriven;
import com.wf.smartcomm.server.bean.House;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.service.HouseService;
import com.wf.smartcomm.server.service.UserService;
import com.wf.smartcomm.server.util.ConstantUtils;
import com.wf.smartcomm.server.util.IdUtils;
import com.wf.smartcomm.server.util.StringUtil;

/** 
 * @author 38884306@qq.com  
 * @version 创建时间：2015-5-28 下午7:04:26 
 * 
 */
@Controller(value="adminUserAction")
@Scope("prototype")
public class UserAction extends BaseAction implements ModelDriven<User>{
	
	private User user = new User();
	
	@Resource
	private UserService userServiceImpl;
	
	@Resource
	private HouseService houseServiceImpl;
	
	public void listOwner() throws Exception{
		User manager = (User) session.getAttribute("loginUser");
		String aoData = request.getParameter("aoData");
		String sEcho = "";
		String iDisplayStart = "";
		String iDisplayLength = "";
		String sSearch = "";
		
		JSONObject jo = new JSONObject(aoData);
		sEcho = jo.getString("sEcho");
		iDisplayStart = jo.getString("iDisplayStart");
		iDisplayLength = jo.getString("iDisplayLength");
		sSearch = jo.getString("sSearch");
		
		page.setBeginIndex(Integer.parseInt(iDisplayStart));
		page.setPageSize(Integer.parseInt(iDisplayLength));
		if (!StringUtil.isEmpty(sSearch)) {
			userServiceImpl.queryPrimaryByKey(manager.getLoginVillage(),sSearch, page);
		}else {
			userServiceImpl.queryByPage(manager.getLoginVillage(),page);
		}
		
		int  initEcho = Integer.parseInt(sEcho)+1;
		int count = page.getTotalCount();
		HashMap<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("iTotalRecords", count);
		dataMap.put("sEcho",initEcho);
		dataMap.put("iTotalDisplayRecords", count);
		dataMap.put("aaData", page.getDataList());
		printJsonData(outGson.toJson(dataMap));
	}
	
	public void listAttach() throws Exception{
		User manager = (User) session.getAttribute("loginUser");
		String hostId = request.getParameter("hostId");
		String aoData = request.getParameter("aoData");
		String sEcho = "";
		String iDisplayStart = "";
		String iDisplayLength = "";
		int  initEcho = 0;
		String sSearch = "";
		JSONObject jo = new JSONObject(aoData);
		sEcho = jo.getString("sEcho");
		iDisplayStart = jo.getString("iDisplayStart");
		iDisplayLength = jo.getString("iDisplayLength");
		sSearch = jo.getString("sSearch");
		page.setBeginIndex(Integer.parseInt(iDisplayStart));
		page.setPageSize(Integer.parseInt(iDisplayLength));
		initEcho = Integer.parseInt(sEcho)+1;
		
		if (!StringUtil.isEmpty(sSearch)) {
			userServiceImpl.queryAttachByKey(manager.getLoginVillage(),sSearch, page);
		}else {
			userServiceImpl.listAttach(manager.getLoginVillage(),hostId,page);
		}
		
		int count = page.getTotalCount();
		HashMap<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("iTotalRecords", count);
		dataMap.put("sEcho",initEcho);
		dataMap.put("iTotalDisplayRecords", count);
		dataMap.put("aaData", page.getDataList());
		printJsonData(outGson.toJson(dataMap));
	}
	
	public void listProperty() throws Exception{
		User manager = (User) session.getAttribute("loginUser");
		String sEcho = "";// 记录操作的次数  每次加1
		String iDisplayStart = "";// 起始
		String iDisplayLength = "";// size
		int  initEcho = 0;
		String aoData = request.getParameter("aoData");
		JSONObject jo = new JSONObject(aoData);
		sEcho = jo.getString("sEcho");
		iDisplayStart = jo.getString("iDisplayStart");
		iDisplayLength = jo.getString("iDisplayLength");
		page.setBeginIndex(Integer.parseInt(iDisplayStart));
		page.setPageSize(Integer.parseInt(iDisplayLength));
		initEcho = Integer.parseInt(sEcho)+1;
		userServiceImpl.listProperty(manager.getLoginVillage(),page);
		int count = page.getTotalCount();
		HashMap<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("iTotalRecords", count);
		dataMap.put("sEcho",initEcho);
		dataMap.put("iTotalDisplayRecords", count);
		dataMap.put("aaData", page.getDataList());
		printJsonData(outGson.toJson(dataMap));
	}
	
	public void listAllProperty() throws Exception{
		String sEcho = "";// 记录操作的次数  每次加1
		String iDisplayStart = "";// 起始
		String iDisplayLength = "";// size
		int  initEcho = 0;
		String aoData = request.getParameter("aoData");
		JSONObject jo = new JSONObject(aoData);
		sEcho = jo.getString("sEcho");
		iDisplayStart = jo.getString("iDisplayStart");
		iDisplayLength = jo.getString("iDisplayLength");
		page.setBeginIndex(Integer.parseInt(iDisplayStart));
		page.setPageSize(Integer.parseInt(iDisplayLength));
		initEcho = Integer.parseInt(sEcho)+1;
		userServiceImpl.listProperty(null,page);
		int count = page.getTotalCount();
		HashMap<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("iTotalRecords", count);
		dataMap.put("sEcho",initEcho);
		dataMap.put("iTotalDisplayRecords", count);
		dataMap.put("aaData", page.getDataList());
		printJsonData(outGson.toJson(dataMap));
	}
	
	public void addAttach() throws Exception{
		User pass = userServiceImpl.getUserByUsername(user.getUsername());
		if (pass != null) {
			printJsonData(101);
			return;
		}
		String houseId = request.getParameter("houseId");
		House house = houseServiceImpl.getById(houseId);
		User host = house.getUser();
		if (host == null) {
			printJsonData(403);
			return;
		}
		user.setObjectId(IdUtils.uuid());
		user.setPushId(IdUtils.uuid16());
		user.setCreatedAt(String.valueOf(System.currentTimeMillis()));
		user.setUpdateAt(user.getCreatedAt());
		user.setIsUnReg("0");
		user.setDeviceType(ConstantUtils.DEVICE_TYPE_UNKNOW);
		user.setParent(host);
		user.setType(ConstantUtils.USER_TYPE_ATTACH);
		user.setLoginVillage(house.getVillage());
		userServiceImpl.registe(user);
		houseServiceImpl.addUser(houseId,user.getObjectId());
		printJsonData(200);
	}
	
	public void addPrimary() throws Exception{
		User pass = userServiceImpl.getUserByUsername(user.getUsername());
		if (pass != null) {
			printJsonData(101);
			return;
		}
		User manager = (User) session.getAttribute("loginUser");
		user.setObjectId(IdUtils.uuid());
		user.setPushId(IdUtils.uuid16());
		user.setPassword(IdUtils.MD5(user.getPassword()));
		user.setCreatedAt(String.valueOf(System.currentTimeMillis()));
		user.setUpdateAt(user.getCreatedAt());
		user.setIsUnReg("0");
		user.setDeviceType(ConstantUtils.DEVICE_TYPE_UNKNOW);
		user.setType(ConstantUtils.USER_TYPE_PRIMARY);
		user.setLoginVillage(manager.getLoginVillage());
		userServiceImpl.registe(user);
		printJsonData(200);
	}
	
	public void modifyAccount()throws Exception{
		String account = request.getParameter("account");
		User old = userServiceImpl.getUserById(user.getObjectId());
		old.setUsername(account);
		userServiceImpl.update(old);
		houseServiceImpl.listByHost(old.getObjectId(), page);
		for (House house : (List<House>)page.getDataList()) {
			house.setPhoneNumber(old.getUsername());
			house.setVoipPhoneNumber(old.getUsername());
			houseServiceImpl.update(house);
		}
		printJsonData(200);
	}
	
	public void listHouse() throws Exception{
		System.out.println("param:" + request.getParameter("objectId"));
	}

	@Override
	public User getModel() {
		return user;
	}
	
}
